package com.xiaobawang.homework1225.controller;

import com.xiaobawang.homework1225.entity.JsonResult;
import com.xiaobawang.homework1225.mapper.RoleMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * description：User Controller
 * author：Synhard
 * create time：2019/12/25 15：48
 * last modified: 2019/12/25 15：48
 * **/
@Api(tags = "role")
@RestController
public class RoleController {

    @Autowired
    private RoleMapper roleMapper;

    @ApiOperation(value = "Add role")
    @PutMapping("/addRole")
    public JsonResult addRole(String r_name, String r_remark) {
        JsonResult result = new JsonResult();
        if (roleMapper.selectRoleByName(r_name) != null) {
            // 如果角色名称已经存在，则提示用户添加角色失败
            result.setErrorcode(-1);
            result.setStatus("该角色已存在，添加失败！");
            result.setResults(null);
        } else {
            // 如果角色不存在则尝试插入角色
            if(roleMapper.insertRole(r_name, r_remark) == 1) {
                // 如果插入用户成功
                result.setErrorcode(0);
                result.setStatus("添加角色成功!");
                result.setResults(roleMapper.selectRoleByName(r_name));
            } else {
                // 出现了其他错误
                result.setErrorcode(-2);
                result.setStatus("添加角色失败，出现其他错误！");
                result.setResults(null);
            }
        }
        return result;
    }

    @ApiOperation(value = "Delete role")
    @GetMapping("/deleteRole")
    public JsonResult deleteRole(int r_id) {
        JsonResult result = new JsonResult();
        if (roleMapper.selectUserUnderRole(r_id) != 0) {//该角色下已有用户
            result.setErrorcode(-1);
            result.setStatus("删除失败！该角色下已有用户");
            result.setResults(null);
        } else {//该角色下没有用户
            if (roleMapper.deleteRoleById(r_id) == 1) {//成功删除
                result.setErrorcode(0);
                result.setStatus("删除成功！");
                result.setResults(null);
            } else {//删除失败
                result.setErrorcode(-1);
                result.setStatus("删除失败！未知错误");
                result.setResults(null);
            }
        }
        return result;
    }

    @ApiOperation("Fuzzy select role by name")
    @GetMapping("/fuzzySelectRoleByName")
    public JsonResult fuzzySelectRoleByName(String r_name) {
        JsonResult result = new JsonResult();
        result.setErrorcode(0);
        result.setStatus("查询成功！");
        result.setResults(roleMapper.fuzzySelectRoleByName(r_name));
        return result;
    }

    @ApiOperation("Select user`s name, email. role, create time")
    @GetMapping("/selectRoleInfo")
    public JsonResult selectRoleInfo() {
        JsonResult result = new JsonResult();
        result.setStatus("查询成功！");
        result.setErrorcode(0);
        result.setResults(roleMapper.selectRoleInfo());
        return result;
    }

}
